#include <gl/freeglut.h>
#include "RailGun.h"
#include <cmath>
#include "Duck.h"

	double xDegrees;
	double yDegrees;
	double xDuckLocation;
	double yDuckLocation;
	double zDuckLocation;

	GLuint display_list_handle_gun;

	/*
	//Displays the body of the gun by shifting 4 rods length 5.9 (to avoid z fighting)
	//to different corners of the x-y square
	*/
	void RailGun::GunBody(void)
	{
		//	Joint creates a sphere of radius 0.5 located at the current frame's origin.
		glClearColor(1.0,1.0,1.0,0.0);
		glColor4f(.5,.5,.5,.5);
		glTranslatef(-.333f, 0, 0);
		glPushMatrix();
		glScalef(.333f, .333f, 5.9f);
		glutSolidCube(1);
		glPopMatrix();
		glTranslatef(.666f, 0, 0);
		glPushMatrix();
		glScalef(.333f, .333f, 5.9f);
		glutSolidCube(1);
		glPopMatrix();
		glTranslatef(0, .666f, 0);
		glPushMatrix();
		glScalef(.333f, .333f, 5.9f);
		glutSolidCube(1);
		glPopMatrix();
		glTranslatef(-.666f, 0, 0);
		glPushMatrix();
		glScalef(.333f, .333f, 5.9f);
		glutSolidCube(1);
		glPopMatrix();
		glTranslatef(.333f, -.333f, 0);
		glPushMatrix();
		glTranslated(0, 1, -1);
		glPushMatrix();
		glTranslated(0,0, 1);
		glPopMatrix();
		glPopMatrix();

	}

	/*
	//Creates a square to put on the end of a gun (.999 x .999) to make it the same dimension as the body x-y square
	*/
	void RailGun::GunEnd(void)
	{
		//	Joint creates a sphere of radius 0.5 located at the current frame's origin.
		glClearColor(1.0,1.0,1.0,0.0);
		glColor4f(.5,.5,.5,.5);

		glPushMatrix();
		glScalef(1.0f, 1.0f, .3f);
		glutSolidCube(.999);
		glPopMatrix();
	}

	/*
	//Call to the railgun object to display the gun body and 2 gun ends in the proper fashion
	//
	//inGame:			bool determining whether to rotate the gun as if being in the game
	//elapsed_time:		double to determine speed when gun is not inGame
	*/
	void RailGun::DisplayRailGun(bool inGame, double elapsed_time)
	{
		//global location	
		glPushMatrix();
		glTranslatef(0,0,-6);
		glTranslated(0, -1, 0);

		//If the gun is inGame, rotate by the amount it got passed in from the game
		if(inGame)
		{
			glTranslated(0, 0, 2);
			glRotated(yDegrees, 0, 1, 0);
			glRotated(xDegrees, 1, 0, 0);
			glTranslated(0, 0, -1);
		} 

		//Rotate by the time that has elapsed
		else 
		{
			glRotated(elapsed_time, 0, 1, 0);
		}

		//If the display list has not been initialized, then it creates the display list and draws manually
		if(display_list_handle_gun == (GLuint)-1)
		{
			display_list_handle_gun = glGenLists(2);
			glNewList(display_list_handle_gun, GL_COMPILE);

			//Displays the gun parts to create an actual gun
			GunBody();
			glTranslatef(0, 0, 2.85f);
			GunEnd();
			glTranslatef(0, 0, -5.7f);
			GunEnd();
			glPopMatrix();
			glEndList();
		}
		glCallList(display_list_handle_gun);
		glPopMatrix();
	}

	/*
	//Constructor for the RailGun object
	*/
	RailGun::RailGun(void)
	{
		display_list_handle_gun = -1;
		xDegrees = 0;
		yDegrees = 0;
		xDuckLocation = 0;
		yDuckLocation = 0;
		zDuckLocation = 0;
	}

	//Set the x rotation of the gun
	void RailGun::setXDegrees(double d){
		xDegrees = d;
	}

	//Get the x rotation of the gun
	double RailGun::getXDegrees(void){
		return xDegrees;
	}

	//Set the y rotation of the gun
	void RailGun::setYDegrees(double d){
		yDegrees = d;
	}

	//Get the y rotation of the gun
	double RailGun::getYDegrees(void){
		return yDegrees;
	}
